<?php

namespace App\Admin\Controllers;


use App\Exceptions\BizException;
use App\Member;
use App\RiskRule;
use Encore\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class AddRule extends Form
{
    /**
     * The form title.
     *
     * @var string
     */
    public $title = '新增规则';

    /**
     * Handle the form request.
     *
     * @param Request $request
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request)
    {

        $dengji = $request->input('dengji',0);
        $gzmc = $request->input('gzmc','');

        $data = $request->input();

        try{
            $risk_rule = new RiskRule();
            if(empty($dengji)){
                throw new BizException('等级不能为空');
            }
            if(empty($gzmc)){
                throw new BizException('规则名称不能为空');
            }else{
                $is = RiskRule::whereGzmc($gzmc)->first();
                if($is){
                    throw new BizException('规则名称不能重复');
                }
            }

            $up_data = [
                'dengji' => $dengji,
                'gzmc' => $gzmc,
                'sort' => $data['sort'],
                'name' => $data['name'],
            ];
            $result = RiskRule::conversionData($data);
            if(!$result['status']){
                //处理失败
                throw new BizException($result['msg']);
            }
            //合并增加数组
            $up_data = array_merge($up_data,$result['up_data']);
            $up_data['z_name'] = $result['z_name'];
            $up_data['ultimately'] = json_encode($result['arr']);

            DB::table('risk_rules')->insert($up_data);
            return back()->withInput();
        }catch (\Exception $e){
            admin_error($e->getMessage());
            return back()->withInput();
        }

        admin_success('添加成功');
        return redirect(sprintf('%s/members',config('admin.route.prefix')));
    }

    /**
     * Build a form here.
     */
    public function form()
    {


        $this->select('dengji','等级')->rules('required')->default(1)->options(RiskRule::DENGJI_MAP);
        $this->text('gzmc','规则名称')->required();
        $this->text('name','名称');
        $this->text('money','金额限制')->help("金额限制在2000-4000只需填写:2000-4000 金额大于7000填:>,7000 其他符号有>,>=,==,-,<,");
        $this->text('age','年龄限制');

        $this->text('courtdetail','法院信息详情-个人高级版')
            ->help('例子失信被执行人身份证+匹配度≥0.65: shixin,>=,0.65 (失信被执行人:shixin 执行公告被执行人:zxgg 法院公告:fygg 曝光台:bgt 网贷黑名单:wdhmd)');

        $this->text('c3','3C申请意向-3C租赁')
            ->help('身份证/按手机号查询，近3个月在3C机构申请总次数≥9次:m3.id.allnum,>=,9&m3.cell.allnum,>=,9 数据解析 时间.查询类型.数据类型');

        $this->text('zxf','租信分');
        $this->text('zzf','租智分');
        $this->text('thqzdj','团火欺诈等级');
        $this->text('jdyxyz','借贷意向验证')->help('近15天在非银机构申请次数 >= 10:d15.id.nbank.allnum,>=,10  银行机构:bank 非银机构:nbank');
        $this->text('fxsb','风险哨兵')->help('风险哨兵90以上（三个维度均满足趋势异常,消费行为预测,履约意愿）:qsyc,>=,90&xfxwyc,>=,90&lyyy,>=,90');
        $this->text('tsmdyz','特殊名单验证')->help('输入类型编号例如:odr0000342');
        $this->text('fybzxr','反欺诈规则-法院被执行人限高版')->help('输入类型编号例如:odr0000210');

        $this->text('ady','自定义')->help('特殊情况可以再这儿输入');
        $this->number('sort','排序')->help('越小越靠前');

        $this->display('ultimately','最终综合条件');
    }

    /**
     * The data of the form.
     *
     * @return array $data
     */
    public function data()
    {
        $request = new Request();
        $id = $request->input('id');
        $data = [
            'dengji' => 1,
            'gzmc' => 'asdasd'

        ];
        return $data;
        return old();
    }


}
